/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.lizg.Controller;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.net.InetAddress;

/**
 *
 * @author LizG
 */
public class MessSend {

    private String ip_receive;
    private String filename;
    DatagramSocket ds;
    DatagramPacket dp;

    public void setIp_receive(String ip_receive) {
        this.ip_receive = ip_receive;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }
    //public void sendMess() {
    public void sendMess() {
        // 创建发送端的Socket对象
        //public void sendMess() {
        // 创建发送端的Socket对象
        try {
            ds = new DatagramSocket();
            // 封装键盘录入数据
            // 创建数据并打包
            String mess = "@send"+(String)Inet4Address.getLocalHost().getHostAddress() +
                    "$" + filename;
            dp = new DatagramPacket(mess.getBytes(), mess.getBytes().length,
                    InetAddress.getByName(ip_receive), 8189);
            // 发送数据
            ds.send(dp);
            
        } catch (IOException e) {
            System.out.println("udp发送错误");
        } finally {
            ds.close();
        }
    }
}
